﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Data.OleDb;
using Remind.IDAL;
using Remind.Model;
using Remind.DBUtility;

namespace Remind.AccessDAL
{
    public class Address:IAddress
    {
        protected const string SQL_SELECT_ADDRESS_BY_CID = "SELECT cid,family, company, other FROM address where cid=@cid";
        protected const string SQL_INSERT_ADDRESS = "INSERT INTO address (family, company, other,cid) VALUES( @family, @company, @other,@cid)";

        protected const string PARM_CID = "@cid";

        #region IAddress 成员

        public AddressInfo GetAddressByCid(int cid)
        {
            OleDbParameter parm = new OleDbParameter(PARM_CID, OleDbType.Integer);
            parm.Value = cid;
            using (OleDbDataReader rdr = AccessHelper.ExecuteReader(AccessHelper.ConnectionString, CommandType.Text, SQL_SELECT_ADDRESS_BY_CID, parm))
            {
                AddressInfo address = null;
                if (rdr.Read())
                {
                    address = new AddressInfo();
                    if (!rdr.IsDBNull(0))
                        address.Cid = rdr.GetInt32(0);
                    if (!rdr.IsDBNull(1))
                        address.Family = rdr.GetString(1);
                    if (!rdr.IsDBNull(2))
                        address.Company = rdr.GetString(2);
                    if (!rdr.IsDBNull(3))
                        address.Other = rdr.GetString(3);
                }
                return address;
            }
        }

        public int UpdateAddress(AddressInfo address,Idu idu)
        {
            int resoult = 0;
            OleDbParameter[] parms ={
                                         new OleDbParameter("@family",OleDbType.VarChar,256),
                                         new OleDbParameter("@company",OleDbType.VarChar,256),
                                         new OleDbParameter("@other",OleDbType.VarChar,256),
                                         new OleDbParameter("@cid",OleDbType.Integer)
                                    };
            parms[0].Value = address.Family;
            parms[1].Value = address.Company;
            parms[2].Value = address.Other;
            parms[3].Value = address.Cid;
            switch (idu)
            {
                case Idu.insert:
                    resoult = AccessHelper.ExecuteNonQuery(AccessHelper.ConnectionString, CommandType.Text, SQL_INSERT_ADDRESS, parms);
                    break;
                case Idu.update:
                    break;
                case Idu.delete:
                    break;
                default:
                    break;
            }
            return resoult;
        }
        
        #endregion
    }
}
